//
//  ServerView.m
//  PetAPP
//
//  Created by dllo on 16/1/24.
//  Copyright © 2016年 Yuming. All rights reserved.
//

#import "ServerView.h"
#import "CellOfServer.h"
#import "NetworkingHandle.h"
#import <YYWebImage.h>
#import <DKNightVersion.h>

@interface ServerView () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableDictionary *dataDic;

@property (nonatomic, retain) NSArray *imageArr;

@property (nonatomic, retain) UIImageView *imageViewOfNight;


@end

@implementation ServerView

- (void)dealloc {

    [_tableView release];
    [_dataDic release];
    [_imageArr release];
    [_imageViewOfNight release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self createSubViewsWithFrame:(CGRect){.size = frame.size}];
        [self handleData];
        self.imageArr = @[@[@"wangmeirong", @"miaomeirong", @"jiajiyang"], @[@"wangmeirongNight.jpg", @"miaomeirongNight.jpg", @"jiajiyangNight.jpg"]];
        
        
    }
    
    return self;
}
- (void)handleData {

    [NetworkingHandle handleWorkData:@"http://api.leepet.com/?m=O2O&a=serverIndexInfo" AndBody:@"service=serverIndexInfo&sign=4a609fe4d88aea8da3e6f3e18d6c99c8&uid=1418177" value:@{@"Content-Type" : @"application/x-www-form-urlencoded"} handleBlock:^(id result) {
        NSArray *tempArr = [[result objectForKey:@"data"] objectForKey:@"banner"];
        self.dataDic = [NSMutableDictionary dictionary];
        for (NSString *key in tempArr[0]) {
            if ([key isEqualToString:@"coverpath"]) {
                [self.dataDic setObject:[tempArr[0] objectForKey:key] forKey:key];
            }
            if ([key isEqualToString:@"link"]) {
                [self.dataDic setObject:[tempArr[0] objectForKey:key] forKey:key];
            }
            
        }
        [self.tableView reloadData];
    }];
    
    
}

- (void)createSubViewsWithFrame:(CGRect)frame {
    
    self.tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
    [self addSubview:_tableView];
    [_tableView release];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[CellOfServer class] forCellReuseIdentifier:@"poolOfCell"];

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    view.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:0.934 green:0.934 blue:0.934 alpha:1.0], [UIColor colorWithRed:0.2952 green:0.2952 blue:0.2952 alpha:1.0]);
    
    return view;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0)
        return 0;
    else
        return 25;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 0)
        return 140;
    else
        return 112;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {


    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    CellOfServer *cell = [tableView dequeueReusableCellWithIdentifier:@"poolOfCell"];
    if (indexPath.section == 0) {
//        cell.imageViewOfServer.yy_imageURL = [NSURL URLWithString:[self.dataDic objectForKey:@"coverpath"]];
        [cell.imageViewOfServer yy_setImageWithURL:[NSURL URLWithString:[self.dataDic objectForKey:@"coverpath"]] options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation];

    }else{

        cell.imageViewOfServer.dk_imagePicker = DKImageWithNames(self.imageArr[0][indexPath.section - 1], self.imageArr[1][indexPath.section - 1]);
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"AlertNone" object:nil];

}


@end
